using System;

using System.Linq;
using System.Text;
using System.Transactions;
using System.Collections.Generic;
using Entity;
using Common.Background.Entity;
using System.Data.Linq;

namespace Common.Background.Data
{

    /// <summary>
    /// 
    /// </summary>
    public class WebManageUser_WebManageRolesRepository : BackgroundRepositoryBase, IWebManageUser_WebManageRolesRepository
    {
        IWebManageUsersRepository iWebManageUsersRepository = null;
        IWebManageRolesRepository iWebManageRolesRepository = null;
        public WebManageUser_WebManageRolesRepository()
        {
            iWebManageUsersRepository = new WebManageUsersRepository();
            iWebManageRolesRepository = new WebManageRolesRepository();
        }

        #region IWebManageUser_WebManageRolesRepository 成员



        public IQueryable<WebManageUser_WebManageRoles> GetDetailModel()
        {
            var linq = from data in this.GetModel()
                       join data2 in iWebManageUsersRepository.GetModel() on data.ManageUserID equals data2.ManageUserID
                       join data3 in iWebManageRolesRepository.GetModel() on data.ManageRoleID equals data3.ManageRoleID
                       select new WebManageUser_WebManageRoles_Ext
                       {
                           ManageRoleID = data.ManageRoleID,
                           ManageUserID = data.ManageUserID,
                           WebManageRoles_Extend = data3,
                           WebManageUsers_Extend = data2,
                        };

            return linq;
        }

        public void DeleteForUserID(int userid)
        {
            List<WebManageUser_WebManageRoles> list = new List<WebManageUser_WebManageRoles>();
            this.GetModel().Where(i => i.ManageUserID == userid).ToList().ForEach(i =>
            {
                list.Add(new WebManageUser_WebManageRoles
                {
                    ManageRoleID = i.ManageRoleID,
                    ManageUserID = i.ManageUserID,
                 });
            });
            this.Delete(list);
        }
        #endregion

        #region IRepository<WebManageUser_WebManageRoles> 成员

        public void Update(WebManageUser_WebManageRoles entity)
        {
            base.UpdateEntity(entity);
        }

        public void Update(IList<WebManageUser_WebManageRoles> list)
        {
            base.UpdateEntity(list.Cast<EntityBase>().ToList());
        }

        public void Insert(WebManageUser_WebManageRoles entity)
        {
            base.InsertEntity(entity);
        }

        public void Insert(IList<WebManageUser_WebManageRoles> list)
        {
            base.InsertEntity(list.Cast<EntityBase>().ToList());
        }

        public WebManageUser_WebManageRoles InsertGetIDENTITY(WebManageUser_WebManageRoles entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.ManageUserID, entity.ManageUserID);
        }

        public void Delete(WebManageUser_WebManageRoles entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<WebManageUser_WebManageRoles> list)
        {
            base.DeleteEntity(list.Cast<EntityBase>().ToList());
        }

        public IQueryable<WebManageUser_WebManageRoles> GetModel()
        {
            return base.GetEntities<WebManageUser_WebManageRoles>().Select(data => new WebManageUser_WebManageRoles_Ext
            {
                ManageRoleID = data.ManageRoleID,
                ManageUserID = data.ManageUserID,
             });
        }

        public WebManageUser_WebManageRoles Find(params object[] keyValues)
        {
            return base.GetEntity<WebManageUser_WebManageRoles>(keyValues);
        }

        #endregion
    }
}
